package com.itheima.thread.stampedlock;

import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.StampedLock;

/**
 * @Author: Ethan Yankang
 * @Program: Interview
 * @Date: 2024-08-21 23:53
 **/
public class StampedLockTest {
    public static void testStampedLock() throws Exception{
    final StampedLock lock = new StampedLock();
    Thread thread01 = new Thread(()->{
        // 获取写锁
        lock.writeLock();
        // 永远阻塞在此处，并且不释放写锁
        LockSupport.park();
    });
    thread01.start();
    // 保证thread01获取写锁[启动线程执行之]
    Thread.sleep(100);

    Thread thread02 = new Thread(()->
                           //阻塞在悲观读锁
                           lock.readLock()
                          );
    thread02.start();
    // 保证T2阻塞在读锁
    Thread.sleep(100);
    //中断线程thread02
    //会导致线程thread02所在CPU飙升
    thread02.interrupt();
    //表示先把线程02执行完了在执行主线程
    thread02.join();
}

    public static void main(String[] args) throws Exception {
        testStampedLock();
    }
}
